package cn.bigfire.wzdj.util;

import cn.bigfire.wzdj.pojo.vo.PCInfo;
import cn.hutool.core.util.StrUtil;
import cn.hutool.system.HostInfo;
import cn.hutool.system.OsInfo;
import cn.hutool.system.UserInfo;
import cn.hutool.system.oshi.OshiUtil;
import lombok.extern.slf4j.Slf4j;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HWDiskStore;
import oshi.hardware.HWPartition;
import oshi.hardware.NetworkIF;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
public class PCInfoUtil {

    public static OsInfo osInfo = new OsInfo();
    public static UserInfo userInfo = new UserInfo();
    public static HostInfo hostInfo = new HostInfo();
    public static PCInfo pcInfo = new PCInfo();

    static {
        try {
            String osArch = osInfo.getArch();
            String osName = osInfo.getArch();
            String userName = userInfo.getName();
            String userHomeDir = userInfo.getHomeDir();
            String userCurrDir = userInfo.getCurrentDir();
            String userTempDir = userInfo.getHomeDir();
            String userCountry = userInfo.getCountry();
            String hostName = hostInfo.getName();
            pcInfo.setOsArch(osArch);
            pcInfo.setOsName(osName);
            pcInfo.setUserName(userName);
            pcInfo.setHostName(userHomeDir);
            pcInfo.setUserCurrDir(userCurrDir);
            pcInfo.setUserTempDir(userTempDir);
            pcInfo.setUserCountry(userCountry);
            pcInfo.setHostName(hostName);
            //处理器信息
            CentralProcessor processor = OshiUtil.getProcessor();
            String name = processor.getProcessorIdentifier().getName();
            int pCount = processor.getPhysicalProcessorCount();
            int lCount = processor.getLogicalProcessorCount();
            String processorInfo = String.format("%s (%s物理核 %s逻辑核)", name, pCount, lCount);
            pcInfo.setProcessor(processorInfo);
            //内存信息
            String memoryInfo = OshiUtil.getMemory().toString() + "(" + OshiUtil.getMemory().getPhysicalMemory().size() + "根)";
            pcInfo.setMemory(memoryInfo);
            //磁盘信息

            List<String> diskList = new ArrayList<>();
            for (HWDiskStore diskStore : OshiUtil.getDiskStores()) {
                StringBuilder diskInfo = new StringBuilder();
                String diskName = StrUtil.subBefore(diskStore.getModel(), "(", false);
                String diskSize = ByteUtil.getSize(diskStore.getSize());
                StringBuilder partSb = new StringBuilder();
                for (HWPartition part : diskStore.getPartitions()) {
                    partSb.append(part.getMountPoint()).append(ByteUtil.getSize(part.getSize()));
                }
                diskInfo.append(diskName).append("|").append(diskSize).append("|").append(partSb.toString());
                diskList.add(diskInfo.toString());
            }
            pcInfo.setDiskList(diskList);//三部分 名字,大小,盘符和大小
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
//        log.info(osInfo);
//        log.info(userInfo);
//        log.info(hostInfo);
//
//        log.info(systemInfo.getHardware());
//        log.info(systemInfo.getOperatingSystem());
//        log.info(NetUtil.getLocalMacAddress());
//        SystemUtil.dumpSystemInfo();
//        log.info("getCpuInfo------------------------------------------");
//        log.info(OshiUtil.getCpuInfo());//CPU
//        log.info("处理器----------------------------------------------------------------------------");
//        log.info(OshiUtil.getProcessor());//处理器
//        CentralProcessor processor = OshiUtil.getProcessor();
//        String name = processor.getProcessorIdentifier().getName();
//        int pCount = processor.getPhysicalProcessorCount();
//        int lCount = processor.getLogicalProcessorCount();
//        String processorInfo = String.format("%s (%s物理核 %s逻辑核)", name, pCount, lCount);
//        log.info(processorInfo);
//        log.info("内存----------------------------------------------------------------------------");
//        String memoryInfo = OshiUtil.getMemory().toString() + "(" + OshiUtil.getMemory().getPhysicalMemory().size() + "根)";
//        log.info(memoryInfo);//内存
//        log.info("磁盘----------------------------------------------------------------------------");
//        StringBuilder diskInfo = new StringBuilder();
//        for (HWDiskStore diskStore : OshiUtil.getDiskStores()) {
//            StringBuilder partSb = new StringBuilder();
//            String diskName = StrUtil.subBefore(diskStore.getModel(), "(", false);
//            String diskSize = ByteUtil.size(diskStore.getSize());
//            for (HWPartition part :  diskStore.getPartitions()) {
//                partSb.append(part.getMountPoint() + ByteUtil.size(part.getSize()));
//            }
//            String diskPartition = partSb.toString();
//            diskInfo.append(diskName).append("|").append(diskSize).append("|").append(diskPartition).append("\n");
//        }
//        log.info(diskInfo);//三部分 名字,大小,盘符和大小
//        log.info("getNetworkIFs----------------------------------------------------------------------------");
//        for (NetworkIF networkIF : OshiUtil.getNetworkIFs()) {
//            log.info(networkIF);
//        }
//        NetworkIF networkIF = getNetworkIF();
//        if (networkIF == null) {
//            log.info("无网环境");
//        } else {
//            log.info(networkIF);
//            long mtu = networkIF.getMTU();
//            log.info(mtu);
//        }


//        int networkType = getNetworkType();
//        log.info(networkType);
//        log.info("getSensors----------------------------------------------------------------------------");
//        log.info(OshiUtil.getSensors());//温度
    }

    public static NetworkIF getNetworkIF() {
        for (NetworkIF networkIF : OshiUtil.getNetworkIFs()) {
//            log.info(networkIF.getMTU());
//            if (networkIF.getIPv4addr().length > 0) {
//                return networkIF;
//            }
        }
        return null;
    }

    public static int getNetworkType() {
        try {
            List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface intf : interfaces) {//循环第一个网卡，看是以太网还是WIFI
                try {

                    if (intf.getInetAddresses().hasMoreElements()) {
                        log.info(intf.getName() +"     "+ intf.getDisplayName());
                        List<InterfaceAddress> interfaceAddresses = intf.getInterfaceAddresses();
                        for (InterfaceAddress interfaceAddress : interfaceAddresses) {
                            InetAddress address = interfaceAddress.getAddress();
                        }
                        InetAddress inetAddress = intf.getInetAddresses().nextElement();
                        log.info(inetAddress.toString());
                    }


                } catch (Exception e) {
                    log.error(e.getMessage());
                    continue;
                }

//                if (intf.getName().startsWith("wlan")) {
//                    return 2;
//                } else if (intf.getName().startsWith("eth")) {
//                    return 1;
//                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}


    /*
Name: wlan3 (Broadcom 802.11n Network Adapter #2) [IfAlias=WLAN 2]
  MAC Address: f4:b7:e2:d2:50:db
  MTU: 1500, Speed: 144000000
  IPv4: []
  IPv6: [fe80:0:0:0:2be6:3768:c66:27fc/64]
  Traffic: received 25957 packets/23.6 MiB (0 err, 0 drop); transmitted 143288 packets/12.9 MiB (0 err, 0 coll);
Name: wlan4 (Microsoft Wi-Fi Direct Virtual Adapter #3) [IfAlias=本地连接* 1]
  MAC Address: f6:b7:e2:d2:50:db
  MTU: 1500, Speed: 0
  IPv4: []
  IPv6: [fe80:0:0:0:6a89:58cf:3d4c:dd6a/64]
  Traffic: received 0 packets/0 bytes (0 err, 0 drop); transmitted 0 packets/0 bytes (0 err, 0 coll);
Name: eth4 (Realtek PCIe GbE Family Controller) [IfAlias=以太网]
  MAC Address: b8:88:e3:e8:8a:de
  MTU: 1500, Speed: 0
  IPv4: []
  IPv6: [fe80:0:0:0:b5bf:fc7a:52c0:4f14/64]
  Traffic: received 0 packets/0 bytes (0 err, 0 drop); transmitted 0 packets/0 bytes (0 err, 0 coll);
2

Name: wlan3 (Broadcom 802.11n Network Adapter #2) [IfAlias=WLAN 2]
  MAC Address: f4:b7:e2:d2:50:db
  MTU: 1500, Speed: 144000000
  IPv4: [192.168.31.63/24]
  IPv6: [fe80:0:0:0:2be6:3768:c66:27fc/64]
  Traffic: received 458 packets/178.4 KiB (0 err, 0 drop); transmitted 385 packets/259.7 KiB (0 err, 0 coll);
Name: wlan4 (Microsoft Wi-Fi Direct Virtual Adapter #3) [IfAlias=本地连接* 1]
  MAC Address: f6:b7:e2:d2:50:db
  MTU: 1500, Speed: 0
  IPv4: []
  IPv6: [fe80:0:0:0:6a89:58cf:3d4c:dd6a/64]
  Traffic: received 0 packets/0 bytes (0 err, 0 drop); transmitted 0 packets/0 bytes (0 err, 0 coll);
Name: eth4 (Realtek PCIe GbE Family Controller) [IfAlias=以太网]
  MAC Address: b8:88:e3:e8:8a:de
  MTU: 1500, Speed: 0
  IPv4: []
  IPv6: [fe80:0:0:0:b5bf:fc7a:52c0:4f14/64]
  Traffic: received 0 packets/0 bytes (0 err, 0 drop); transmitted 0 packets/0 bytes (0 err, 0 coll);
2

     */
